﻿using System;
using System.IO;
using System.Windows.Forms;

namespace EA_therapy
{
    public partial class DZ : Form
    {
        String path = Application.StartupPath.ToString();
        Panel father;
        public DZ(Panel _father)
        {
            InitializeComponent();
            father = _father;
            label1.Text = "本次治疗号为：" + Global.disID;
            linkLabel1.Text = path;
        }

        private void open_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" 保存文件时请保存在选择路径，治疗单号填写上述治疗号！");
            if (!Directory.Exists(path + @"\电阻采集\" + DateTime.Now.ToLongDateString()))
            {
                Directory.CreateDirectory(path + @"\电阻采集\" + DateTime.Now.ToLongDateString());
            }
            System.Diagnostics.Process.Start(Application.StartupPath.ToString() + @"\电阻采集\BodyADp.exe");
        }

        private void read_Click(object sender, EventArgs e)
        {
            int time = 0;
            string strLine;
            if (!Global.eoc)
                time = 1;
            else
                time = 2;
            try
            {
                for (int i = 1; i <= time; i++)
                {
                    double dz = 0.0;
                    int count = 0;
                    String temppath = path + @"\电阻采集\" + DateTime.Now.ToLongDateString() + "\\" + Global.disID + "-" + i.ToString();
                    FileStream aFile = new FileStream(temppath, FileMode.Open);
                    StreamReader sr = new StreamReader(aFile);
                    strLine = sr.ReadLine();
                    while (strLine != null)
                    {
                        string[] sArray = strLine.Split('\t');
                        strLine = sr.ReadLine();
                        dz += Convert.ToDouble(sArray[1]);
                        count++;
                    }
                    if (i == 1)
                    {
                        string info = "治疗前电阻='" + dz / count + "'";
                        EADB.UpdateDZ(info);
                    }
                    if (i == 2)
                    {
                        string info = "治疗后电阻='" + dz / count + "'";
                        EADB.UpdateDZ(info);
                    }
                    sr.Close();
                    MessageBox.Show("读取成功！");
                    
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine("An IOException has been thrown!");
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
                return;
            }
        }

        private void cute_Click(object sender, EventArgs e)
        {
            string info = Global.disID + "','";
            info += DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
            EADB.AddTip(info);
            MessageBox.Show("填写治疗单号时请填写上述治疗号！");
            Global.eoc = true;
            System.Diagnostics.Process.Start(Application.StartupPath.ToString() + @"\电针治疗仪\ELTR.exe");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            main newform = new main(father);
            newform.FormBorderStyle = FormBorderStyle.None;
            newform.TopLevel = false;
            newform.ControlBox = false;
            newform.Dock = DockStyle.Fill;
            this.father.Controls.Add(newform);
            newform.Show();
            this.Close();
        }

        private void DZ_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            String[,] info = new String[18, 2];
            info[0, 0] = "治疗单号";
            info[1, 0] = "治疗时间";
            info[2, 0] = "频率1";
            info[3, 0] = "频率2";
            info[4, 0] = "治疗结束时间";
            info[5, 0] = "治疗日期";
            info[6, 0] = "通道1波形";
            info[7, 0] = "通道1最大值";
            info[8, 0] = "通道1最小值";
            info[9, 0] = "通道2波形";
            info[10, 0] = "通道2最大值";
            info[11, 0] = "通道2最小值";
            info[12, 0] = "通道3波形";
            info[13, 0] = "通道3最大值";
            info[14, 0] = "通道3最小值";
            info[15, 0] = "通道4波形";
            info[16, 0] = "通道4最大值";
            info[17, 0] = "通道4最小值";
            string strLine;
            try
            {
                String temppath = Directory.GetCurrentDirectory() + @"\电针治疗仪\" + DateTime.Now.ToLongDateString() + "\\" + Global.disID + ".txt";
                StreamReader sr = new StreamReader(temppath, System.Text.Encoding.Default);
                strLine = sr.ReadLine();
                if (strLine != null)
                {
                    string[] sArray = strLine.Split(' ');
                    for (int i = 0; i < 18; i++)
                        info[i, 1] = sArray[i];
                }
                EADB.UpdateTip(info);
            }
            catch (IOException ex)
            {
                Console.WriteLine("An IOException has been thrown!");
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
                return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                path = folderBrowserDialog1.SelectedPath;
            linkLabel1.Text = path;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(Global.disID);
            MessageBox.Show("已复制！");
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", path);
        }
    }
}
